/*
 * @(#)BackPropMom.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


package xfuzzy.xfsl.model.algorithm;

import xfuzzy.xfsl.model.*;
import xfuzzy.lang.*;
import xfuzzy.xfds.XfdsDataSet;

/**
 * Algoritmo de retropropagaci�n de errores con momento
 * 
 * @author Francisco Jos� Moreno Velo
 * @see "Jacobs, R., Increased rates of convergence through learning rate adaptation, 
 * Neural Networks, Vol. 1, N. 4, pp. 295-308, 1988."
 *
 */
public class BackPropMom extends XfslGradientBasedAlgorithm {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Factor de aprendizaje
	 */
	private double rate;
	
	/**
	 * Factor de momento
	 */
	private double momentum;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public BackPropMom() {
		this.rate = 0.1;
		this.momentum = 0.7;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Devuelve el c�digo de identificaci�n del algoritmo
	 */
	public int getCode() {
		return BACKPROPMOM;
	}

	/**
	 * Actualiza los par�metros de configuraci�n del algoritmo
	 */
	public void setParameters(double[] param) throws XflException {
		if(param.length != 2) throw new XflException(26);
		rate = test(param[0], POSITIVE);
		momentum = test(param[1], MOMENTUM);
	}

	/**
	 * Obtiene los par�metros de configuraci�n del algoritmo
	 */
	public XfslAlgorithmParam[] getParams() {
		XfslAlgorithmParam[] pp = new XfslAlgorithmParam[2];
		pp[0] = new XfslAlgorithmParam(rate, 0.1, POSITIVE, "Learning Rate");
		pp[1] = new XfslAlgorithmParam(momentum, 0.7, MOMENTUM, "Momentum Factor");
		return pp;
	}

	/**
	 * Ejecuta una iteraci�n del algoritmo
	 */
	public XfslEvaluation iteration(Specification spec, XfdsDataSet pattern,
			XfslErrorFunction ef) throws XflException {

		XfslEvaluation prev = computeErrorGradient(spec,pattern,ef);
		Parameter[] param = spec.getAdjustable();
		for(int i=0; i<param.length; i++) {
			double desp = -rate*param[i].getDeriv()+momentum*param[i].getPrevDesp();
			param[i].setDesp(desp);
			param[i].setPrevDeriv(param[i].getDeriv());
			param[i].setDeriv(0);
		}

		spec.update();
		return ef.evaluate(spec,pattern,prev.error);
	}
}

